//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;

namespace CommunityServer.Components
{

	/// <summary>
	/// Manager class for InkData
	/// </summary>
	public class Ink
	{
		private Ink(){}

		public static int Add(InkData data)
		{
			data = CommonDataProvider.Instance().CreateUpdateDeleteInkData(data, DataProviderAction.Create);
			CSCache.Insert("Ink:" + data.InkID.ToString(), data,CSCache.MinuteFactor*2);
			return data.InkID;
		}

		public static void Update(InkData data)
		{
			CommonDataProvider.Instance().CreateUpdateDeleteInkData(data, DataProviderAction.Update);
			CSCache.Remove("Ink:" + data.InkID.ToString());
			CSCache.Insert("Ink:" + data.InkID.ToString(), data,CSCache.MinuteFactor*2);
		}

		public static void Delete(InkData data)
		{
			CSCache.Remove("Ink:" + data.InkID.ToString());
			CommonDataProvider.Instance().CreateUpdateDeleteInkData(data, DataProviderAction.Delete);
		}

		public static InkData Get(int inkID)
		{
			InkData data = CSCache.Get("Ink:" + inkID.ToString()) as InkData;
			if(data == null)
			{
				data = CommonDataProvider.Instance().GetInkData(inkID);
				CSCache.Insert("Ink:" + inkID.ToString(), data,CSCache.MinuteFactor*2);
			}

			if(data != null)
			{
				CSApplicationData iApp = ApplicationSet.Applications[data.ApplicationType];	
				Section section = iApp.GetSectionByID(data.SectionID);
				if(section == null)
					throw new CSException(CSExceptionType.SectionNotFound, "The Ink's Section could not be found");

				Permissions.ValidatePermissions(section, Permission.View,CSContext.Current.User);
					
			}

			return data;
		}
	}


}
